package com.dwxt.cms.controller;


import com.dwxt.cms.entity.Permission;
import com.dwxt.cms.service.PermissionServiceAdmin;
import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/permission")
@RestController
public class PermissionControllerAdmin {

    @Autowired
    private PermissionServiceAdmin permissionServiceAdmin;

    @PostMapping("/createOrUpdatePermission")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "url", notNull = true),
            @ValidateFiled(index = 0, filedName = "category", notNull = true),
    })
    public BaseResult createOrUpdatePermission(Permission permission) {
        return permissionServiceAdmin.createOrUpdatePermission(permission);
        //return i == 0 ? BaseResult.build(StatusCode.PERMISSION_EXIST) : BaseResult.ok();
    }

    @DeleteMapping("/delete")
    public BaseResult delete(Integer[] id) {
        int i = permissionServiceAdmin.batchDeleteById(id);
        return i == 0 ? BaseResult.build(StatusCode.PERMISSION_NOT_EXIST) : BaseResult.ok();
    }

    @GetMapping("/pageList")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            @ValidateFiled(index = 1, notNull = true),
    })
    public BaseResult pageList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Permission> permissions = permissionServiceAdmin.selectAll();
        PageInfo<Permission> pageInfo = new PageInfo<>(permissions);
        return BaseResult.ok(pageInfo);
    }

    @GetMapping("/categoryList")
    public BaseResult categoryList() {
        List<String> categoryList = permissionServiceAdmin.selectDistinctCategoryList();
        return BaseResult.ok(categoryList);
    }
}
